/**
 * 
 */
package com.gmail.blackdog1987.carcare.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmail.blackdog1987.carcare.dao.MaintainDao;
import com.gmail.blackdog1987.carcare.dao.MaintainDetailDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.carcare.entity.TMaintain;
import com.gmail.blackdog1987.carcare.entity.TMaintainDetail;
import com.gmail.blackdog1987.carcare.entity.base.Page;
import com.gmail.blackdog1987.carcare.service.MaintainService;
import com.gmail.blackdog1987.carcare.util.ErrorCode;

/**
 * 
 * @author 董国超
 *
 */
@Service("maintainService")
public class MaintainServiceImpl implements MaintainService {
	
	@Autowired
	private MaintainDao maintainDao;
	@Autowired
	private MaintainDetailDao maintainDetailDao;
	@Autowired
	private BaseJdbcDao baseJdbcDao;

	@Override
	public Map<String, Object> getList(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String type = filter.get("type");
		String pageindex = filter.get("pageindex");
		String pagesize = filter.get("pagesize");
		String lon = filter.get("lon");
		String lat = filter.get("lat");
		int index = 1;
		int size = 999;
		if(pageindex!=null&&pageindex.length()!=0){
			index = Integer.valueOf(pageindex);
		}
		
		if(index<1){
			index = 1;
		}
		if(pagesize!=null&&pagesize.length()!=0){
			size = Integer.valueOf(pagesize);
		}
		if(lon==null||lon.length()==0){
			lon = "0";
		}
		if(lat==null||lat.length()==0){
			lat = "0";
		}
		if(type==null||type.length()==0){
			type = "";
		}
		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		sql = " from t_maintain where type=? order by getDistance(?, ?, lat, lon) ";
		whereArgs.add(type);
		whereArgs.add(lon);
		whereArgs.add(lat);
		
		String limit = " limit " + (index-1)*size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" select count(*) " + sql , whereArgs.toArray());
		
		List<TMaintain> maintains = JdbcUtil.queryForList(baseJdbcDao, TMaintain.class, " select * " + sql + limit, whereArgs.toArray());
		
		Page page = new Page(maintains, index, totalCount, size);
		
		result.put("result", page);
		return result;
	}
	
	@Override
	public Map<String, Object> getDetailList(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String maintainid = filter.get("maintainid");
		String sql = "select * from t_maintain_detail where maintain_id=? ";
		
		List<TMaintainDetail> details = JdbcUtil.queryForList(baseJdbcDao, TMaintainDetail.class, sql, new Object[]{maintainid});
		result.put("result", details);
		return result;
	}
}
